using System;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using CE.Database;

/// <summary>
/// Summary description for CompanyDB
/// </summary>
/// 
namespace CE.Company
{
    public class CompanyDB
    {
        public CompanyDB()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        public Int64 Insert(Company company)
        {
            MyDatabase _db = new MyDatabase();
            Int64 _result = -1;
            string _spName = "sprocProviderCompanyInsertUpdateSingleItem";
            
            SortedList _sPara = new SortedList();
            
            #region Parameter
                _sPara.Add("@ProviderCompanyId", company.ProviderCompanyId);
                _sPara.Add("@CompanyName", company.CompanyName);
                _sPara.Add("@BusInsType", company.BusIndType);
                _sPara.Add("@CompanyType", company.CompanyType);
                _sPara.Add("@PhoneNo1", company.PhoneNo1);
                _sPara.Add("@PhoneNo2", company.PhoneNo2 );
                _sPara.Add("@FaxNo1", company.FaxNo1);
                _sPara.Add("@FaxNo2", company.FaxNo2);
                _sPara.Add("@Email1", company.Email1);
                _sPara.Add("@Email2", company.Email2);
                _sPara.Add("@URL1", company.URL1);
                _sPara.Add("@URL2", company.URL2);
                _sPara.Add("@Street", company.Street);
                _sPara.Add("@UnitNo1", company.UnitNo1);
                _sPara.Add("@UnitNo2", company.UnitNo2);
                _sPara.Add("@PostCode", company.PostCode);
                _sPara.Add("@City", company.City);
                _sPara.Add("@Country", company.Country);
                _sPara.Add("@Status", company.Status);
                _sPara.Add("@OtherContact1", company.OtherContact1);
                _sPara.Add("@OtherContact2", company.OtherContact2);
                //_sPara.Add("@CompanyLogoSmall1", company.CompanyLogoSmall1);
                //_sPara.Add("@CompanyLogoServerUrl", company.CompanyLogoServerUrl);
                _sPara.Add("@CompanyColorCode1", company.CompanyColorCode1);
                _sPara.Add("@CompanyColorCode2", company.CompanyColorCode2);
                _sPara.Add("@CompanyColorCodeRGB", company.CompanyColorCodeRGB);
                _sPara.Add("@RefCEUserId", company.RefCEUserId);
                _sPara.Add("@Info1", company.Info1);
                _sPara.Add("@Info2", company.Info2);
                _sPara.Add("@Info3", company.Info3);
                _sPara.Add("@Info4", company.Info4);
                _sPara.Add("@Info5", company.Info5);
                _sPara.Add("@Info6", company.Info6);
                _sPara.Add("@Info7", company.Info7);
                _sPara.Add("@Info8", company.Info8);
                _sPara.Add("@Info9", company.Info9);
                _sPara.Add("@Info10", company.Info10);
                _sPara.Add("@Info11", company.Info11);
                _sPara.Add("@Info12", company.Info12);
                _sPara.Add("@Info13", company.Info13);
                _sPara.Add("@Info14", company.Info14);
                _sPara.Add("@Info15", company.Info15);
                _sPara.Add("@Info16", company.Info16);
                _sPara.Add("@Info17", company.Info17);
                _sPara.Add("@Info18", company.Info18);
                _sPara.Add("@Info19", company.Info19);
                _sPara.Add("@Info20", company.Info20);
            #endregion

            _result = _db.ExecuteSP(_spName, _sPara);

            return _result;
        }

        public Int64 CheckCompanyName(string CompanyName)
        {
            MyDatabase _Db = new MyDatabase();
            string _strSQL = "Select  CX_DB_Live.. ProviderCompanyId from tblProviderCompany Where CompanyName = @CompanyName";
            
            SortedList _sPara = new SortedList();

            _sPara.Add("@CompanyName", CompanyName);
            object _objresult = _Db.ExecuteScalarSqlStatement(_strSQL, _sPara);
            Int64 _result = 0;
            
            if (_objresult != null)
            {
                _result = Convert.ToInt64(_objresult.ToString());
            }

            return _result;
        }

        public Company GetCompany(Int64 ProviderCompanyId)
        {
            MyDatabase _Db = new MyDatabase("GGR_DBConnectionString");
            string _spName = "sprocProviderCompanySelectSingleItem";
            Company _result = null;
            DataTable _dt;
            SortedList _sPara = new SortedList();

            _sPara.Add("@ProviderCompanyId", ProviderCompanyId);

            _dt = _Db.GetDataTableBySP(_spName, _sPara);

            if (_dt != null && _dt.Rows.Count > 0)
            {
                _result = new Company();
                _result = FillDataRecord(_dt);
            }

            return _result;
        }

        private Company FillDataRecord(DataTable myDataTable)
        {
            Company myCompany = new Company();
            myCompany.ProviderCompanyId = Convert.ToInt64(myDataTable.Rows[0]["ProviderCompanyId"].ToString());
            myCompany.CompanyName = myDataTable.Rows[0]["CompanyName"].ToString();
            myCompany.BusIndType = myDataTable.Rows[0]["BusInsType"].ToString();
            myCompany.CompanyType = myDataTable.Rows[0]["CompanyType"].ToString();
            myCompany.PhoneNo1 = myDataTable.Rows[0]["PhoneNo1"].ToString();
            myCompany.PhoneNo2 = myDataTable.Rows[0]["PhoneNo2"].ToString();
            myCompany.FaxNo1 = myDataTable.Rows[0]["FaxNo1"].ToString();
            myCompany.FaxNo2 = myDataTable.Rows[0]["FaxNo2"].ToString();
            myCompany.Email1 = myDataTable.Rows[0]["Email1"].ToString();
            myCompany.Email2 = myDataTable.Rows[0]["Email2"].ToString();
            myCompany.URL1 = myDataTable.Rows[0]["URL1"].ToString();
            myCompany.URL2 = myDataTable.Rows[0]["URL2"].ToString();
            myCompany.UnitNo1 = myDataTable.Rows[0]["UnitNo1"].ToString();
            myCompany.UnitNo2 = myDataTable.Rows[0]["UnitNo2"].ToString();
            myCompany.PostCode = myDataTable.Rows[0]["PostCode"].ToString();
            myCompany.Street = myDataTable.Rows[0]["Street"].ToString();
            myCompany.City = myDataTable.Rows[0]["City"].ToString();
            myCompany.Country = myDataTable.Rows[0]["Country"].ToString();
            myCompany.CompanyColorCode1 = myDataTable.Rows[0]["CompanyColorCode1"].ToString();
            myCompany.CompanyColorCode2 = myDataTable.Rows[0]["CompanyColorCode2"].ToString();
            myCompany.CompanyColorCodeRGB = myDataTable.Rows[0]["CompanyColorCodeRGB"].ToString();
            myCompany.CompanyNameDescription = myDataTable.Rows[0]["CompanyNameDescription"].ToString();
            //myCompany.CompanyLogoSmall1 = (byte[])myDataTable.Rows[0]["CompanyLogoSmall1"];
            //myCompany.CompanyLogoServerUrl = myDataTable.Rows[0]["CompanyLogoServerUrl"];
            return myCompany;
        }
    }
}